
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

/*
webview
 */
class OtherWebViewStatePage extends XPageWebViewState{

  String title;
  String webUrl;
  dynamic jsonData;

  OtherWebViewStatePage(
      {
        super.desktopNewWindowTitle='睿本云HD',
        required this.title,
        required this.webUrl,
        required this.jsonData,
        super.isFinishOverrideWebviewGoback=false,
        super.isCleanAllCookies=false,
        super.openDevTools=false,
      }
      );

  @override
  String getWebUrl() {
    return webUrl;
  }

  @override
  void onStart() {
    XFlutterUtils.setScreenOrientation(false);
    //保持设备唤醒状态
    XFlutterUtils.keepWakelock();
    setXTitle(titleText: title,isShowLeftWidget: false);

    //半屏
    setXLeftFixSpace(
      width:458,
      replaceDefaultOnTap: (){
        finishPage(result: '');
      },
    );

  }

  //
  @override
  Future<void> handleReceiveJsSendMsg(dynamic jsonStr) async {
    try{
      Map<String,dynamic> jsMsgMap=XJsonUtils.json2Map(jsonStr);
      String type=jsMsgMap['type'];
      //h5封装方法约定为json字符串
      dynamic data=jsMsgMap['data'];

      if('openNewWebViewComplete'==type){
        XLogUtils.printLog('---h5通知NewWebView加载完成,jsonData原样通知h5');

        if(jsonData!=null){
          if(jsonData is String){
            runJSFunction(jsFunction_getAppValue('NewWebViewJsonData',jsonData));
          }else{
            runJSFunction(jsFunction_getAppValue('NewWebViewJsonData',XJsonUtils.obj2Json(jsonData)));
          }
        }

      }else if('closeNewWebView'==type){
        finishPage(result: data);
      }

    }catch(error,stacktrace){
      XLogUtils.printLog('---error$error,stacktrace=$stacktrace');
    }
  }

  //////////////////////////////////////////////////

  //原生调用js方法
  String jsFunction_getAppValue(String key,String value){
    return "javascript:getAppValue('$key','$value')";
  }


  //////////////////////////////////////////////////




  @override
  void onNotifyWebViewLoadUrlError(int? errorCode, String? description, String? failingUrl) {
    //webview加载url失败通知
    XLogUtils.printLog("---webview加载url失败通知: failingUrl=$failingUrl");
    setState(() {
      isWebViewLoadFailShowCustomWidget=true;
    });

  }

  @override
  Widget getUrlLoadFailCustomWidget() {
    return XWidgetUtils.getWidgetGestureDetector(
        child: XWidgetUtils.getLayoutContainer(
          alignment:Alignment.center,
          backgroundColor: Colors.white,
          child: XWidgetUtils.getLayoutColumn(
              mainAxisSize:MainAxisSize.min,
              mainAxisAlignment:MainAxisAlignment.center,
              crossAxisAlignment:CrossAxisAlignment.center,
              children: [
                XImageUtils.getImageWidgetWithAsset(
                    'index_no_data_5.png',
                    width: 122,
                    height: 98
                ),
                XWidgetUtils.getWidgetText(
                  '网络不可用,请检查网络连接,点击刷新重试!',
                  padding: XWidgetUtils.getEdgeInsets(0, 10, 0, 0),
                )
              ]
          ),
        ),
        onTap: (){
          //加载失败自定义提示界面,点击重新加载
          XLogUtils.printLog("-----reLoadUrl");
          setState(() {
            isWebViewLoadFailShowCustomWidget=false;
            reLoadUrl(loadFailUrl);
          });
        }
    );
  }



}